import api from "@/api/switchManage"
import { Message } from 'iview'

export default {
    state: {  
        switchTables: {
            "domain_id": '',
            "switch": [ 
            ]
        },
        switchBandwidth: [],
        customerRelatedomains: [],
        switchIndexes: {}
    },
    getters: {  
        customerRelatedomains: state => state.customerRelatedomains, 
        switchTables: state => state.switchTables, 
        switchBandwidth: state => state.switchBandwidth, 
        switchIndexes: state => state.switchIndexes, 
    },
    actions: {  
        fetchCustomerRelateDomain({commit}, condition){  
            api.fetchCustomerRelateDomain(condition.params).then(reply => { 
                if(reply.data.ok){
                    commit('FETCH_CUSTOMER_RELATE_DOMAIN', reply.data.data);
                    condition.cb && condition.cb()   
                }else{
                    Message.error(reply.data.msg || "请求失败")
                } 
            })
        },
        fetchDomainSwitchList({commit}, condition){ 
            api.fetchDomainSwitchList(condition.params).then(reply => { 
                if(reply.data.ok){
                    commit('FETCH_DOMAIN_SWITCH_LIST', reply.data.data); 
                }else{
                    Message.error(reply.data.msg || "请求失败")
                } 
            })
        },
        queryDomainSwitchData({commit}, condition){ 
            api.queryDomainSwitchData(condition.params).then(reply => { 
                if(reply.data.ok){
                    commit('QUERY_DOMAIN_SWITCH_DATA', reply.data.data); 
                    condition.cb && condition.cb(reply.data)  
                }else{
                    condition.cb && condition.cb({ok: false})  
                    Message.error(reply.data.msg || "请求失败")
                } 
            })
        },
        createDomainSwitch({commit}, condition){ 
            api.createDomainSwitch(condition.params).then(reply => {   
                condition.cb && condition.cb(reply.data)
            })
        }, 
        putDomainSwitch({commit}, condition){ 
            api.putDomainSwitch(condition.params).then(reply => {   
                condition.cb && condition.cb(reply.data)
            })
        }, 
        deleteDomainSwitch({commit}, condition){ 
            api.deleteDomainSwitch(condition.params).then(reply => {  
                condition.cb && condition.cb(reply.data)
                if(reply.data.ok){ 
                    commit('EMPTY_DOMAIN_SWITCH_DATA');
                }
            })
        },
        querySwitchIndexes({commit}, condition){ 
            api.querySwitchIndexes(condition).then(reply => { 
                if(reply.data.ok){
                    commit('QUERY_SWITCH_INDEXES', {data: reply.data.data, host : condition.host}); 
                }else{
                    Message.error(reply.data.msg || "获取汇聚口失败")
                } 
            }).catch(reply => {
                
                commit('QUERY_SWITCH_INDEXES', {data: [], host:  condition.host}); 
            })
        }, 
    },
    mutations: { 
        FETCH_CUSTOMER_RELATE_DOMAIN(state, data){
            state.customerRelatedomains = [...data]
        }, 
        FETCH_DOMAIN_SWITCH_LIST(state, data){
            state.switchTables = Object.assign({}, data)
        }, 
        QUERY_DOMAIN_SWITCH_DATA(state, data){
            state.switchBandwidth = data
            //模拟数据
            // state.switchBandwidth = [["2019-03-31 15:05",60],["2019-03-31 15:00",125],["2019-03-31 16:00",25],["2019-03-31 15:10",0]]
        },
        EMPTY_DOMAIN_SWITCH_DATA(state){
            state.switchTables.switch = []
        },
        QUERY_SWITCH_INDEXES(state, data){
            let d = data.data,
                host = data.host;
            // d = {
            //     "XGigabitEthernet0/0/1": "6",
            //     "XGigabitEthernet0/0/10": "15",
            //     "XGigabitEthernet0/0/11": "16",
            //     "XGigabitEthernet0/0/12": "17",
            //     "XGigabitEthernet0/0/13": "18",
            //     "XGigabitEthernet0/0/14": "19",
            //     "XGigabitEthernet0/0/15": "20"
            // }
            let indexes = [];
            for(let es in d){
                indexes.push({
                    label: es,
                    value: d[es]
                })
            }
            state.switchIndexes = Object.assign({}, state.switchIndexes, {[host]: indexes})
        },
        EMPTY_SWITCH_INDEXES(state){ 
            state.switchIndexes = {}
        }
    }
}